package com.zzty.txj.controller;


import com.zzty.txj.entity.Ability;
import com.zzty.txj.entity.User;
import com.zzty.txj.service.AbilityService;
import com.zzty.txj.service.DoDailyService;
import com.zzty.txj.service.UserService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/ability")
public class AbilityController {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(AbilityController.class);

    @Autowired
    DoDailyService doDailyService;

    @Autowired
    UserService userService;

    @Autowired
    AbilityService abilityService;

    @RequestMapping("/addAbilityByUserId")
    public String addAbilityByUserId(User user) {
        String result = "false";
        logger.info("AbilityController addAbilityByUserId user: "+user.toString());
        User user1 = userService.getUserByOpenId(user.getOpenId());
        logger.info("AbilityController getUserByOpenId user1: "+user1.toString());
        Ability ability=abilityService.getAbilityByUserId(user1.getId());
        if(ability==null){
            Integer count = abilityService.addAbility(user1);
            if (count == 1) {
                result = "success";
            }
        }
        return result;
    }

    @RequestMapping("/getAbilityByUserId")
    public Ability getAbilityByUserId(Integer userId) {
        Ability ability =abilityService.getAbilityByUserId(userId);
        if (ability == null) {
            return null;
        } else {
            return ability;
        }
    }

    @RequestMapping(value = "/getRanking", method = RequestMethod.GET)
    public Map getRanking(Long[] userIds) {
        Map<Object, Object> map = new HashMap<>();
        List<Ability> abilities=new ArrayList<>();

        for (int i = 0; i < userIds.length; i++) {
            int userId = userIds[i].intValue();

            Ability ability =abilityService.getAbilityByUserId(userId);
            if (ability == null) {
                continue;
            } else {
                User user = userService.getUserById(userId);

                String userName = user.getName();
                ability.setUserName(userName);


                double performAbility = ability.getPerformAbility();
                double credibility = ability.getCredibility();
                double studyAbility = ability.getStudyAbility();
                double talkAbility = ability.getTalkAbility();
                double laziness = ability.getLaziness();
                Integer self = new Integer((int) (performAbility + credibility + studyAbility + talkAbility + laziness));

                ability.setSort(self);
                abilities.add(ability);

            }
        }

        Collections.sort(abilities, new Comparator<Ability>() {
            @Override
            public int compare(Ability o1, Ability o2) {
                // 按照sort进行升序排列
                if (o1.getSort() > o2.getSort()) {
                    return 1;
                }
                if (o1.getSort() == o2.getSort()) {
                    return 0;
                }
                return -1;
            }
        });
        map.put("ranking",abilities);
        return map;
    }
}
